<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Team extends HYD_Controller {
    private $_search_field_arr  = array();
    
    public function __construct(){
        parent::__construct();
        $this->load->model('team/team_model');
        $this->field_lists_init();
    }
    
    private function field_lists_init(){  
        //搜索中的“查询条件”选单                 
        $this->_search_field_arr = array('name'=> '商品名称',
                                         'description'=> '商品描述',
                                         'price'=> '商品价格');
    }
    
    public function list_team(){
        //$module = $this->input->get('module');
        //$action = $this->input->get('action');
        //log_message('error', $module.'-'.$action.'-begin index-'.microtime());
        $sql_str = '';
        $start = $this->input->get_post('start') ? $this->input->get_post('start') : 0;
        $limit = $this->input->get_post('limit') ? $this->input->get_post('limit') : MAX_DISPLAY_SEARCH_RESULTS;
        $search_type = $this->input->get_post('search_type');
        if($search_type == 'button'){
            $search_content = $this->input->get_post('search_content');
            $sql_str .= 'classid='.$search_content;
        }elseif($search_type == 'text'){
            $search_content = $this->input->get_post('search_content');
            if (strlen(trim($search_content)) > 0) {
                $value = addslashes($search_content);
                $search_field_arr = array_keys($this->_search_field_arr);
                foreach ($search_field_arr as $field) {
                    if ($sql_str){
                        $sql_str .= ' OR ';
                    }
                    $sql_str .= "{$field} like '%{$value}%'";
                }
            }
        }
        
        $team = $this->team_model->get_lists($start, $limit, $sql_str);     
        $response = array(EXT_JSON_READER_TOTAL => $this->team_model->get_total(),
                          EXT_JSON_READER_ROOT => $team);
        
        $this->output->set_content_type('application/json')->set_output(json_encode($response));
        //return $response;
    }
    
    public function get_province_select(){
        $this->load->library('province_city');
        $res = $this->province_city->build_province_select();   
        return $res;  
    }
    public function get_city_select(){
        $this->load->library('province_city');
        $provinceid = $this->input->get_post('provinceid');
        $res = $this->province_city->build_city_select($provinceid);   
        return $res;  
    }
    public function get_team_select(){
        $cityid = $this->input->get_post('cityid');
        if($cityid){
            $team = $this->team_model->get_lists(NULL,NULL,'cityid='.$cityid);
        }else{
            $team = $this->team_model->get_lists();
        }
        $team_select[] = array();
        foreach ($team as $key => $value) {
            $team_select[$key]['id'] = $value['id'];
            $team_select[$key]['text'] = $value['name'];
        }
        return $team_select;
    }
    
    public function save_team(){
        $this->load->helper('form');
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('name', '团队名称', 'required');
        $this->form_validation->set_rules('provinceid', '所属省', 'integer');
        $this->form_validation->set_rules('cityid', '所属城市', 'integer');
        $this->form_validation->set_rules('teamtotal', '团队人数', 'integer');
        //$this->form_validation->set_rules('buildtime', '成立日期', 'required');
        $this->form_validation->set_rules('status', '团队状态', 'integer');
        
        $id = $this->input->get_post('id');
        
        if ($this->form_validation->run() === FALSE){//对以上验证做判断
            echo "{\"failure\":true,feedback:\"提交信息不合法!\"}";
        }else{
            if($id>0){
                $this->team_model->update_team();//转入MODEL
                if(1){
                    echo "{\"success\":true,feedback:\"更新成功!\"}";
                }else{
                    echo "{\"failure\":true,feedback:\"更新失败!\"}";
                }
            }else{
                //echo $this->input->post("id");
                $this->team_model->add_team();//转入MODEL
                if(1){
                    echo "{\"success\":true,feedback:\"添加成功!\"}";
                }else{
                    echo "{\"failure\":true,feedback:\"添加失败!\"}";
                }
            }
        }
    }
}

/* End of file admin.php */
/* Location: ./application/controllers/admin.php */